import {
  deleteThemeComponents,
  getThemeComponent,
  listThemeComponents
} from "@/api/service/theme-coms/theme.coms";
import router from "@/router";

import { defaultTemplateParamsOptions } from "@/data/options/options";
import { form, resetForm, showForm } from "./form";
import { keys, query, table } from "./table";

export const list = async () => {
  table.value.loading = true;
  const data = await listThemeComponents(query.value);
  table.value.data = data.rows;
  query.value.total = data.total;
  table.value.loading = false;
};

export const edit = async (id?: string) => {
  const targetId = id || keys.value[0];
  const { data } = await getThemeComponent(targetId);
  form.value = data;
  if (!form.value.templateList || form.value.templateList.length === 0) {
    form.value.templateList = [...defaultTemplateParamsOptions];
  } else {
    form.value.templateList = data.templateList;
  }
  showForm.value = true;
};
export const create = async () => {
  resetForm();
  router.push({
    path: "/paint",
    query: {
      lcd: "1"
    }
  });
};
export const del = async (id?: string) => {
  const ids = id ? [id] : keys.value;
  await deleteThemeComponents(ids);
  await list();
  keys.value = [];
};

export const search = () => {
  query.value.pageNum = 1;
  list();
};
